package com.fragment4;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.common.base.BaseFragment;
import com.common.route.RouterPath;
import com.fragment4.databinding.MineBinding;
import com.fragment4.service.UserServiceImpl;


@Route(path = RouterPath.MINE_FRAGMENT)
public class MineFragment extends BaseFragment<MineBinding> {

    @Override
    protected Class<MineBinding> getBindingClass() {
        return MineBinding.class;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        binding.tvTitle.setText("我的");
        binding.btnLogin.setOnClickListener(v -> login());
        updateUserInfo();
        // 初始化功能列表
        initFunctionList();
    }

    @Override
    protected void loadData() {
        // 加载我的页面数据
    }

    private void login() {
        if (UserServiceImpl.getInstance().isLogin()) {
            // 已登录，执行登出
            UserServiceImpl.getInstance().logout();
            updateUserInfo();
            showToast("已退出登录");
        } else {
            // 未登录，跳转到登录页
            ARouter.getInstance().build(RouterPath.LOGIN_ACTIVITY).navigation();
        }
    }

    private void updateUserInfo() {
        if (UserServiceImpl.getInstance().isLogin()) {
            // 已登录状态
            binding.tvUsername.setText("用户名: " + UserServiceImpl.getInstance().getUsername());
            binding.tvUsername.setVisibility(View.VISIBLE);
            binding.btnLogin.setText("退出登录");
        } else {
            // 未登录状态
            binding.tvUsername.setVisibility(View.GONE);
            binding.btnLogin.setText("登录");
        }
    }

    private void initFunctionList() {
        String[] functions = new String[]{
                "我的订单", "收货地址", "我的收藏", "设置"
        };

        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                requireContext(),
                android.R.layout.simple_list_item_1,
                functions
        );

        binding.lvMine.setAdapter(adapter);
        binding.lvMine.setOnItemClickListener((parent, view, position, id) -> {
            if (!UserServiceImpl.getInstance().isLogin()) {
                showToast("请先登录");
                ARouter.getInstance().build(RouterPath.LOGIN_ACTIVITY).navigation();
                return;
            }

            // 根据点击位置处理不同功能
            handleFunctionClick(position);
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        // 每次页面可见时刷新用户信息
        updateUserInfo();
    }

    private void handleFunctionClick(int position) {
        switch (position) {
            case 0: // 我的订单
                ARouter.getInstance().build(RouterPath.ORDER_LIST).navigation();
                break;
            case 1: // 收货地址
                ARouter.getInstance().build(RouterPath.ADDRESS_LIST).navigation();
                break;
            case 2: // 我的收藏
                ARouter.getInstance().build(RouterPath.FAVORITE_LIST).navigation();
                break;
            case 3: // 设置
                ARouter.getInstance().build(RouterPath.SETTINGS).navigation();
                break;
        }
    }

    private void showToast(String message) {
        Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onTabReselected() {
        // 刷新个人信息
        updateUserInfo();
    }
}
